package com.cloudspace.jindun.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.manager.MessageNumberManager;
import com.cloudspace.jindun.model.Qunliao;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.ObjUtil;

public class QunliaoiListAdapter extends MyBaseAdapter<Qunliao.DataBean>{
    public QunliaoiListAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.xiaoxi_qunliao_history_item,parent,false);
        }
        ListViewHolder viewHolder = (ListViewHolder) convertView.getTag();
        if (viewHolder == null) {
            viewHolder = new ListViewHolder(convertView);
            convertView.setTag(viewHolder);
        }

        viewHolder.bindView(getContext(),getItem(position));
        return convertView;
    }
    private static class ListViewHolder{
        private ImageView mImageView;
        private TextView mTitle;

        private ImageView mRedDotView;

        private ListViewHolder(View itemView){
            mImageView= (ImageView) itemView.findViewById(R.id.xiaoxi_image_header);
            mTitle= (TextView) itemView.findViewById(R.id.xiaoxi_tv_name);

            mRedDotView = (ImageView) itemView.findViewById(R.id.reddot_iv);
        }

        public void bindView(final Context context, final Qunliao.DataBean item){
            if (item.getChannelName() != null) {
                mTitle.setText(item.getChannelName());
            }

            if (item.getIconUrl() != null) {
                ImageManager.loadCircleImage(context, mImageView, item.getIconUrl(), Configs.IMAGE_CORNER_RADIUS);
            }

            if (!ObjUtil.isEmpty(item) && !ObjUtil.isEmpty(item.getChannelId())){
                boolean unread = MessageNumberManager.getInstance().isChannelUnread(item.getChannelId());
                if (unread) {
                    mRedDotView.setVisibility(View.VISIBLE);
                } else {
                    mRedDotView.setVisibility(View.GONE);
                }
            }
        }
    }
}
